package app.view;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.graphics.Point;
import android.os.SystemClock;
import android.view.View;

/**
 * @author  Johannes Borchardt
 *
 * Plays gif movie 
 * code taken from: http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/
 */
public class GifMovieView extends View {
			 
    private Movie mMovie;
    private long mMoviestart;

    
    public GifMovieView(Context context, InputStream stream) {
        super(context);
        
        mMovie = Movie.decodeStream(stream);   
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        super.onDraw(canvas);
        final long now = SystemClock.uptimeMillis();

        if (mMoviestart == 0) { 
            mMoviestart = now;
        }
        
        int cw = getWidth();
        int ch = getHeight();
        
        int mw = mMovie.width();
        int mh = mMovie.height();
        
        float xOrigin = (cw - mw) / 2;
        float yOrigin = (ch - mh) / 2;

        final int relTime = (int)((now - mMoviestart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, xOrigin, yOrigin);
        this.invalidate();
    }
}